#include <climits>
class Solution {
public:
    int StrToInt(string str) {
        int res=0;
        int index=0;
        int n=str.length();
        while(index<n){
            if(str[index]==' ')
            {
                index++;
            }
            break;
        }
        int sign=1;
        if(str[index]=='+')
        {
            index++;
        }
        else if(str[index]=='-')
        {
            index++;
            sign=-1;
        }
        if(index==n)
        {
            return 0;
        }
        while(index<n)
        {
            char c=str[index];
            if(c<'0'||c>'9')
            {
                return 0;
            }
            if(res>INT_MAX/10||(res==INT_MAX/10&&(c-'0')>INT_MAX%10))
            {
                return INT_MAX;
            }
            if(res < INT_MIN / 10 || (res == INT_MIN / 10 && (c - '0') > -(INT_MIN % 10)))
            {
                return INT_MIN;
            }
            res=res*10+sign*(c-'0');
            index++;
        }
        return res;
    }
};
